<template>
  <div class="notice-info">
    <div class="info-header">
      <el-button 
        type="primary" 
        :icon="Refresh" 
        @click="refreshNotices"
        :loading="loading"
      >
        刷新公告
      </el-button>
    </div>

    <el-card v-loading="loading" class="info-card">
      <template #header>
        <div class="card-header">
          <el-icon><Bell /></el-icon>
          <span>公告信息 ({{ notices.length }}条)</span>
        </div>
      </template>

      <div v-if="hasNotices" class="notices-content">
        <div 
          v-for="notice in notices" 
          :key="notice.id" 
          class="notice-item"
        >
          <el-card class="notice-card" shadow="hover">
            <div class="notice-header">
              <h3 class="notice-title">{{ notice.title }}</h3>
              <div class="notice-meta">
                <el-tag 
                  :type="getNoticeTypeTag(notice.type)" 
                  size="small"
                >
                  {{ getNoticeTypeText(notice.type) }}
                </el-tag>
                <span class="notice-date">{{ formatDate(notice.publishTime) }}</span>
              </div>
            </div>
            
            <div class="notice-content">
              <p>{{ notice.content }}</p>
            </div>
            
            <div class="notice-footer">
              <div class="notice-author">
                <el-icon><User /></el-icon>
                <span>发布者: {{ notice.publisher || '系统' }}</span>
              </div>
              <div class="notice-actions">
                <el-button 
                  v-if="notice.attachmentUrl" 
                  type="text" 
                  size="small"
                  @click="downloadAttachment(notice)"
                >
                  <el-icon><Download /></el-icon>
                  下载附件
                </el-button>
                <el-button 
                  type="text" 
                  size="small"
                  @click="viewNoticeDetail(notice)"
                >
                  <el-icon><View /></el-icon>
                  查看详情
                </el-button>
              </div>
            </div>
          </el-card>
        </div>

        <!-- 分页 -->
        <div class="pagination-wrapper">
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[5, 10, 20, 50]"
            :total="totalNotices"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>

      <el-empty v-else description="暂无公告信息" />
    </el-card>

    <!-- 公告详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      :title="selectedNotice?.title"
      width="60%"
      :before-close="handleCloseDetail"
    >
      <div v-if="selectedNotice" class="notice-detail">
        <div class="detail-meta">
          <el-descriptions :column="2" border>
            <el-descriptions-item label="公告类型">
              <el-tag :type="getNoticeTypeTag(selectedNotice.type)">
                {{ getNoticeTypeText(selectedNotice.type) }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="发布时间">
              {{ formatDate(selectedNotice.publishTime) }}
            </el-descriptions-item>
            <el-descriptions-item label="发布者">
              {{ selectedNotice.publisher || '系统' }}
            </el-descriptions-item>
            <el-descriptions-item label="重要程度">
              <el-rate 
                v-model="selectedNotice.priority" 
                disabled 
                show-score 
                text-color="#ff9900"
              />
            </el-descriptions-item>
          </el-descriptions>
        </div>
        
        <div class="detail-content">
          <h4>公告内容</h4>
          <div class="content-text">{{ selectedNotice.content }}</div>
        </div>
        
        <div v-if="selectedNotice.attachmentUrl" class="detail-attachment">
          <h4>附件</h4>
          <el-button 
            type="primary" 
            @click="downloadAttachment(selectedNotice)"
          >
            <el-icon><Download /></el-icon>
            下载附件
          </el-button>
        </div>
      </div>
      
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailDialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, onMounted, ref } from 'vue'
import { Bell, User, Download, View, Refresh } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useStudentPortalStore } from '@/stores/studentPortalStore'
import { useUserStore } from '@/stores/user'

const studentPortalStore = useStudentPortalStore()
const userStore = useUserStore()

// 响应式数据
const currentPage = ref(1)
const pageSize = ref(10)
const detailDialogVisible = ref(false)
const selectedNotice = ref(null)

// 计算属性
const loading = computed(() => studentPortalStore.loading)
const notices = computed(() => studentPortalStore.noticeList || [])
const totalNotices = computed(() => notices.value.length)
const hasNotices = computed(() => studentPortalStore.hasNotices)

// 获取公告类型标签样式
const getNoticeTypeTag = (type) => {
  const typeMap = {
    1: 'danger',   // 紧急通知
    2: 'warning',  // 重要通知
    3: 'info',     // 一般通知
    4: 'success'   // 活动通知
  }
  return typeMap[type] || 'info'
}

// 获取公告类型文本
const getNoticeTypeText = (type) => {
  const typeMap = {
    1: '紧急通知',
    2: '重要通知',
    3: '一般通知',
    4: '活动通知'
  }
  return typeMap[type] || '未知类型'
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '未知时间'
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 刷新公告
const refreshNotices = async () => {
  await studentPortalStore.fetchAllNotices()
}

// 分页大小改变
const handleSizeChange = (newSize) => {
  pageSize.value = newSize
  currentPage.value = 1
  refreshNotices()
}

// 当前页改变
const handleCurrentChange = (newPage) => {
  currentPage.value = newPage
  refreshNotices()
}

// 查看公告详情
const viewNoticeDetail = (notice) => {
  selectedNotice.value = notice
  detailDialogVisible.value = true
}

// 关闭详情对话框
const handleCloseDetail = () => {
  detailDialogVisible.value = false
  selectedNotice.value = null
}

// 下载附件
const downloadAttachment = (notice) => {
  if (notice.attachmentUrl) {
    // 创建临时链接下载
    const link = document.createElement('a')
    link.href = notice.attachmentUrl
    link.download = notice.attachmentName || `公告附件_${notice.id}`
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    ElMessage.success('开始下载附件')
  } else {
    ElMessage.warning('该公告没有附件')
  }
}

// 组件挂载时获取数据
onMounted(() => {
  refreshNotices()
})
</script>

<style scoped>
.notice-info {
  padding: 20px;
}

.info-header {
  margin-bottom: 20px;
  display: flex;
  justify-content: flex-end;
}

.info-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
}

.notices-content {
  max-height: 700px;
  overflow-y: auto;
}

.notice-item {
  margin-bottom: 16px;
}

.notice-card {
  border-radius: 8px;
  transition: all 0.3s ease;
}

.notice-card:hover {
  transform: translateY(-2px);
}

.notice-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
}

.notice-title {
  margin: 0;
  color: #2c3e50;
  font-size: 18px;
  font-weight: 600;
  flex: 1;
  margin-right: 16px;
}

.notice-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}

.notice-date {
  color: #666;
  font-size: 12px;
}

.notice-content {
  margin-bottom: 16px;
  line-height: 1.6;
  color: #555;
}

.notice-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 12px;
  border-top: 1px solid #eee;
}

.notice-author {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #666;
  font-size: 14px;
}

.notice-actions {
  display: flex;
  gap: 8px;
}

.pagination-wrapper {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

/* 详情对话框样式 */
.notice-detail {
  padding: 20px 0;
}

.detail-meta {
  margin-bottom: 24px;
}

.detail-content {
  margin-bottom: 24px;
}

.detail-content h4 {
  margin-bottom: 12px;
  color: #2c3e50;
  font-weight: 600;
}

.content-text {
  background: #f8f9fa;
  padding: 16px;
  border-radius: 6px;
  line-height: 1.8;
  color: #555;
  white-space: pre-wrap;
}

.detail-attachment h4 {
  margin-bottom: 12px;
  color: #2c3e50;
  font-weight: 600;
}
</style>